class Solution {
public:
    int ret = 0;
    int m, n, cnt;
    vector<vector<bool>> check;
    int wardrobeFinishing(int _m, int _n, int _cnt) {
        m = _m, n = _n, cnt = _cnt;
        check = vector<vector<bool>>(m + 1, vector<bool>(n + 1));
        dfs(0, 0);
        return ret;
    }
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    void dfs(int i, int j) {
        check[i][j] = true;
        ret++;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            int x1 = x, y1 = y;
            int sum = 0;
            while (x1) {
                sum = sum + x1 % 10;
                x1 /= 10;
            }
            while (y1) {
                sum = sum + y1 % 10;
                y1 /= 10;
            }
            if (x >= 0 && y >= 0 && x < m && y < n && !check[x][y] &&
                sum <= cnt) {
                dfs(x, y);
            }
        }
    }
};